﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class UIFieldRunMapTeamInfo : MonoBehaviour
    {
        public UILabel teamText; 
        public float tickTime;     
        public GameObject template;
        public UIGrid headGrid;
        public UISprite bkSprite;
        
        public List<int> heights;
        public Dictionary<long, GameObject> heroCaches = new Dictionary<long, GameObject>();

        public static bool forceUpdate = false;
        public static int updateTime = 3;

        private int currCount = 0;
        public bool needReposition = false;
        public int CurrCount
        {
            get
            {
                return currCount;
            }
            set
            {
                if (value != currCount)
                {
                    currCount = value;

                    teamText.text = string.Format(TableTextBlo.GetText("field_group_ing"),
                        "("
                        + StaticData.playerData.team.memberList.Count
                        + "/"
                        + StaticData.playerData.team.maxSize
                        + ")");

                    if (currCount > 0)
                    {
                        bkSprite.gameObject.SetActive(true);
                        if (currCount == 1)
                            bkSprite.gameObject.SetActive(false);
                        else if (currCount == 2)
                            bkSprite.height = heights[1];
                        else if (currCount == 3)
                            bkSprite.height = heights[2];
                        else if (currCount == 4)
                            bkSprite.height = heights[3];
                        else if (currCount == 5)
                            bkSprite.height = heights[4];
                    }
                    else
                    {
                        bkSprite.gameObject.SetActive(false);
                    }
                    if (currCount > 1)
                    {
                        RefreshSimpleHeroInfo();
                    }
                }
            }
        }
        
        public void ReRefresh()
        {
            gameObject.SetActive(!string.IsNullOrEmpty(StaticData.playerData.teamId));
            TickMyTeamInfo();
            forceUpdate = true;
        }
        public void OnLookMyTeamInfo()
        {
            var room = Room.RoomManager.currentRoom as Room.RoomField;
            var net = room.netManager as Room.RoomNetField;
            if (net != null)
            {
                net.SendCSQueryTeam();
            }
        }
        
        void TickMyTeamInfo()
        {
            var room = Room.RoomManager.currentRoom as Room.RoomField;
            var net = room.netManager as Room.RoomNetField;
            if (net != null)
            {
                net.SendCSQueryTeamTick();
            }
        }


        void RefreshSimpleHeroInfo()
        {
            if (StaticData.playerData.team != null)
            {
                foreach (var member in StaticData.playerData.team.memberList)
                {
                    if (member.playerId == StaticData.playerData.playerId)
                        continue;

                    if (heroCaches.ContainsKey(member.playerId))
                    {
                        var inst = heroCaches[member.playerId];
                        var script = inst.GetComponent<UIFieldRunMapTeamInfoSimpleHead>();
                        script.Init(member.heroId, member.nickName, (float)member.curHp, member.state == BOOL.TRUE);
                    }
                    else
                    {
                        var inst = Instantiate(template) as GameObject;
                        inst.transform.SetParent(headGrid.transform);
                        inst.transform.localScale = Vector3.one;
                        inst.SetActive(true);
                        heroCaches[member.playerId] = inst;

                        var script = inst.GetComponent<UIFieldRunMapTeamInfoSimpleHead>();
                        script.Init(member.heroId, member.nickName, (float)member.curHp, member.state == BOOL.TRUE);
                        inst.name = member.playerId + "";
                    }
                }

                List<long> villDel = new List<long>();

                foreach (var kvp in heroCaches)
                {
                    bool has = false;
                    foreach (var hero in StaticData.playerData.team.memberList)
                    {
                        if (hero.playerId == kvp.Key)
                        {
                            has = true;
                            break;
                        }
                    }

                    if (!has)
                    {
                        villDel.Add(kvp.Key);
                    }
                }
                foreach (var del in villDel)
                {
                    if (heroCaches.ContainsKey(del))
                    {
                        var obj = heroCaches[del];
                        Destroy(obj);
                        heroCaches.Remove(del);
                    }
                }
            }
            headGrid.Reposition();
            needReposition = true;
        }



        void Update()
        {
            if (StaticData.playerData.team == null)
            {
                teamText.text = TableTextBlo.GetText("field_group_ing0");
                bkSprite.gameObject.SetActive(false);
            }
            else
            {
                CurrCount = StaticData.playerData.team.memberList.Count;
            }

            tickTime -= Time.deltaTime;
            if (tickTime <= 0f)
            {
                TickMyTeamInfo();
                tickTime = updateTime;
            }

            if (forceUpdate)
            {
                RefreshSimpleHeroInfo();
                forceUpdate = false;
                tickTime = updateTime;
            }
            if (needReposition)
            {
                headGrid.Reposition();
                needReposition = false;
            }
        }
    }
}